﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace DeskNotes.Utils
{
    /// <summary>
    /// Class holds icon colored same as DeskNote background 
    /// used in notification area icon menu.
    /// </summary>
    class ColoredTrayIcon
    {
        private Bitmap colored;
        private Color backColor;

        /// <summary>
        /// Gets the bitmap to be used in menu
        /// </summary>
        internal Bitmap Get
        {
            get
            {
                return colored;
            }
        }
        
        /// <summary>
        /// Back color of DeskNote and color used last time for icon painting.
        /// When backcolor is set then icon is repainted from original tray.ico.
        /// </summary>
        internal Color BackColor
        {
            get
            {
                return backColor;
            }
            set
            {
                if (!value.Equals(backColor)){
                    // change icon color
                    backColor = value;
                    colored = SetIconBackground(backColor);
                }
            }
        }

        static internal Bitmap SetIconBackground(Color c)
        {
            int start = Environment.TickCount;
            // Create a new bitmap.

            Bitmap originalBmp = Properties.Resources.tray.ToBitmap();// "C:\\Working\\DeskNotes CodePlex version\\Tests\\tray.ico");

            // Lock the bitmap's bits.  
            Rectangle rect = new Rectangle(0, 0, originalBmp.Width, originalBmp.Height);
            System.Drawing.Imaging.BitmapData bmpData =
                originalBmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                originalBmp.PixelFormat);

            // Get the address of the first line.

            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.

            int bytes = bmpData.Stride * originalBmp.Height;
            byte[] rgbValues = new byte[bytes];

            // Copy the RGB values into the array.

            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);
            // set Yellow to selected color
            for (int i = 0; i < rgbValues.Length - 3; i += 4)
            {
                if (rgbValues[i] == 51) rgbValues[i] = c.B;
                if (rgbValues[i + 1] == 255) rgbValues[i + 1] = c.G;
                if (rgbValues[i + 2] == 255) rgbValues[i + 2] = c.R;
                if (rgbValues[i + 3] == 255) rgbValues[i + 3] = c.A;

            }

            // Set every third value to 255. A 24bpp bitmap will look red.  

            //for (int counter = 2; counter < rgbValues.Length; counter += 3)
            //    rgbValues[counter] = 255;

            // Copy the RGB values back to the bitmap

            System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

            // Unlock the bits.

            originalBmp.UnlockBits(bmpData);

            // Draw the modified image.

            // e.Graphics.DrawImage(bmp, 0, 150);
            //Icon icon = Icon.FromHandle(originalBmp.GetHicon());
            Console.WriteLine("Time elapsed in icon coloring: {0} miliseconds.", Environment.TickCount - start);
            return originalBmp;

        }
    }
}
